﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace HQWeb.SysManage.RoleManage
{
    public partial class RoleUser : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            VerdictIsLogin();
            if (!IsPostBack)
            {
                this.txtIdHid.Text = Request["id"];
                Session["idSign"] = this.txtIdHid.Text.Trim();
                BindListRoleUser();
                BindLabel();

            }
        }
        #region 方法列表
        #region 绑定用户角色列表
        protected void BindListRoleUser()
        {
            BLL.SysManage.T_PUB_USER userBll = new BLL.SysManage.T_PUB_USER();
            GridView1.DataSource = userBll.GetUserInfoByRole(this.txtIdHid.Text);
            GridView1.DataBind();
        }
        #endregion

        #region 绑定角色名称
        protected void BindLabel()
        {
            BLL.SysManage.T_PUB_ROLE roleBll = new BLL.SysManage.T_PUB_ROLE();
            Model.SysManage.T_PUB_ROLE roleModel = new Model.SysManage.T_PUB_ROLE();
            roleModel = roleBll.GetModel(this.txtIdHid.Text.Trim());
            this.ROLENAME.Text = roleModel.ROLE_NAME;
        }
        #endregion
        #endregion

        #region 事件列表
        #region 增加按钮事件
        protected void Add_Click(object sender, EventArgs e)
        {
            //HttpCookie cookie_ROLECODE = new HttpCookie("ROLECODE");
            //cookie_ROLECODE.Value = this.ROLENAME.Text.Trim();
            //Response.AppendCookie(cookie_ROLECODE);

            //Response.Redirect("RoleAddUser.aspx");
            //Response.Write("<script language='javascript'>window.onload=cityResult();</script>");
            //Response.Write("<script language='javascript'>window.open('RoleAddUserFrame.htm','增加人员','width=400','top=400','left=(screen.availwidth-400)/2',height='(screen.availheight-400)/2','location=no,menubar=no,titlebar=no,toolbar=no,status=no,resizable=no');</script>");
            Response.Write("<script language='javascript'>var str=window.showModalDialog('RoleAddUserFrame.htm','增加人员','status:0;dialogHeight:415px;dialogWidth:600px');window.close();window.parent.document.location.reload();</script>");
            //Response.Write("<script language='javascript'>var str=window.showModalDialog('RoleAddUserFrame.htm','增加人员','status:0;dialogHeight:415px;dialogWidth:600px');alert(str);</script>");
            //Response.Write("<script language='javascript'>window.showModalDialog('RoleAddUserFrame.htm','增加人员','status:0;dialogHeight:415px;dialogWidth:600px');</script>");返回值
        }
        #endregion

        #region 行删除
        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            try
            {
                int row = Convert.ToInt32(e.RowIndex);
                int id = Convert.ToInt32(GridView1.Rows[row].Cells[0].Text);
                BLL.SysManage.T_PUB_USERROLE bll = new BLL.SysManage.T_PUB_USERROLE();
                bll.Delete(id);
                BindListRoleUser();
            }
            catch (Exception ex)
            {
                Universal.JS.Alert(ex.Message);
                return;
            }
        }
        #endregion

        #region 翻页事件
        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            //this.GridView1.PageIndex = e.NewPageIndex;
            try
            {
                int newPageIndex = 0;

                if (-2 == e.NewPageIndex)
                {
                    TextBox txtNewPageIndex = null;
                    GridViewRow pagerRow = GridView1.Controls[0].Controls[GridView1.Controls[0].Controls.Count - 1] as GridViewRow; // refer to PagerTemplate
                    if (null != pagerRow)
                    {
                        txtNewPageIndex = pagerRow.FindControl("txtNewPageIndex") as TextBox;   // refer to the TextBox with the NewPageIndex value
                    }

                    if (null != txtNewPageIndex)
                    {
                        newPageIndex = int.Parse(txtNewPageIndex.Text) - 1; // get the NewPageIndex
                    }

                    newPageIndex = newPageIndex < 0 ? 0 : newPageIndex;
                    newPageIndex = newPageIndex >= GridView1.PageCount ? GridView1.PageCount - 1 : newPageIndex;
                    GridView1.PageIndex = newPageIndex;
                }
                else if (e.NewPageIndex == -1)
                {
                    newPageIndex = 0;
                    GridView1.PageIndex = 0;
                }
                else
                {
                    newPageIndex = e.NewPageIndex;
                    GridView1.PageIndex = e.NewPageIndex;
                }
                BindListRoleUser();
            }
            catch (Exception ex)
            {
                Universal.JS.Alert(ex.Message);
                return;
            }
        }
        #endregion
        #endregion

        #region 行绑定触发事件
        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow || e.Row.RowType == DataControlRowType.Header)
            {
                e.Row.Cells[0].Visible = false; //隐藏列,可以取得该隐藏列的信息 
            }
        }
        #endregion
    }
}